package thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

//线程池,重用线程,控制线程
public class ThreadPoolDemo {
    public static void main(String[] args) {
        //创建线程池基不需要创建线程了,里边有线程,而且执行完了不会被销毁而是一直在等待
        ExecutorService pool= Executors.newFixedThreadPool(3);
        for (int i=0;i<5;i++){
            Runnable runnable=new Runnable() {
                @Override
                public void run() {
                    Thread t=Thread.currentThread();
                    System.out.println(t.getName()+":正在执行任务...");
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(t.getName()+"执行完毕");
                }
            };
            //线程池执行一个任务
            pool.execute(runnable);//
            System.out.println("线程池执行一个任务");

        }
        pool.shutdown();
        System.out.println("线程池停止");
    }
}
